<template>
	<view class="content">
		<view class="search-form-inner">
			<view class="search-box">
				<view class="search-icon"></view>
				<input :focus="true" @confirm="onSearchConfirm" @input="onSearchInput" class="input"
					confirmType="search" maxlength="maxLen" name="keyword" :placeholder="preKeyword||'搜素你感兴趣的'"
					placeholderClass="placeholder" type="text" :value="keyword"></input>
				<view @click="clean" class="delete-icon" v-if="keyword.length>0"></view>
			</view>
			<button class="search-btn" @tap="onSearchConfirm" v-if="keyword">搜索</button>
			<button @tap="onCancelBtnClick" class="search-btn" v-else>取消</button>
		</view>
		<view class="hit_search" v-if="searchViewVisible">
			<u-section font-size="36" :show-line="false" title="热门搜索" :right="false"></u-section>
			<view class="hit_words">
				<view @tap="searchStat(item.name)" class="relative" v-for="(item,index) in tags" :key="index">
					<view class="keywords">{{item.name}}</view>
				</view>
			</view>
		</view>
		<view class="resulut" v-if="!searchViewVisible">
			<article-list :posts="posts"></article-list>
			<u-loadmore :status="status" />
		</view>
	</view>
</template>

<script>
	import API from '@/api/api.js'
	export default {
		data() {
			return {
				keyword: '',
				tags: [],
				posts: [],
				page: 1,
				preKeyword: '搜索你感兴趣的内容',
				searchViewVisible: true,
			}
		},
		onLoad() {
			this.getTags();
		},
		onReachBottom() {
			if (!this.isLastPage) {
				this.getPostsList({
					search: this.keyword,
					page: this.page
				})
			}
		},
		methods: {
			getTags: function(args) {
				API.getTags(args).then(res => {
						this.tags = this.tags.concat(res)
					})
					.catch(err => {})
			},
			onSearchInput: function(e) {
				this.keyword = e.detail.value
			},
			searchStat: function(keyword) {
				this.keyword = keyword;
				this.onSearchConfirm();
			},
			onSearchConfirm: function() {
				this.posts = [];
				this.page = 1;
				this.isLastPage = false;
				this.status = "loading";
				this.searchViewVisible = false;
				this.getPostsList({
					search: this.keyword,
					page: this.page
				})
			},
			getPostsList: function(args) {
				API.getPostsList(args).then(res => {
					if (res.length < 10) {
						this.isLastPage = true
						this.status = "nomore"
					}
					this.posts = this.posts.concat(res)
					this.page = this.page + 1
					this.isLoading = false
				}).catch(err => {
					console.log(err)
				})
			},
			clean: function() {
				this.keyword = '';
				this.posts = [];
				this.page = 1;
				this.isLastPage = false;
				this.status = "loading";
				this.searchViewVisible = true;
			},
			onCancelBtnClick: function() {
				uni.navigateBack({
					delta: 1
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.search-form-inner {
		position: fixed;
		left: 0;
		padding: 30rpx;
		height: 100rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		background-color: #fff;
		z-index: 11;

		.search-box {
			flex: 1;
			width: 550rpx;
			height: 60rpx;
			padding: 0 14rpx 0 30rpx;
			border-radius: 30rpx;
			display: flex;
			align-items: center;
			background-color: #f4f3f7;

			.search-icon {
				width: 28rpx;
				height: 28rpx;
				background: url('') no-repeat 0 0;
				background-size: contain;
				margin-right: 18rpx;
			}

			.delete-icon {
				width: 38rpx;
				height: 38rpx;
				background: url('') no-repeat 0 0;
				background-size: contain;
				margin-right: 18rpx;
				z-index: 999;
			}


			.input {
				flex: 1;
				font-size: 28rpx;
				color: #252628;
			}

			.placeholder {
				font-size: 28rpx;
				color: #b7b5c2;
			}
		}

		.search-btn,
		.cancel-btn {
			width: 120rpx;
			height: 60rpx;
			margin-left: 30rpx;
			line-height: 60rpx;
			padding: 0;
			border: none;
			font-size: 28rpx;
			background: #fff;
			color: #434343;
		}

	}

	.hit_search {
		padding: 133rpx 32rpx 0;

		.hit_words,
		.relative {
			display: flex;
			flex-wrap: wrap;
			justify-content: flex-start;
		}

		.keywords {
			color: #6b6a72;
			display: inline-block;
			background: #f4f3f7;
			border-radius: 100rpx;
			padding: 14rpx 33rpx;
			margin: 20rpx 20rpx 0 0;
			max-width: 260rpx;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
			font-size: 22rpx;
		}
	}

	.resulut {
		padding: 100rpx 0rpx;
	}
</style>
